<template>
  <div class="forgot-password-container">
    <!-- 用户信息验证 -->
    <div class="forgot-password-step">
      <div class="forgot-password-card">
        <h2 class="title">找回密码</h2>
        <p class="subtitle">请验证您的身份信息</p>
        
        <el-form 
          ref="verifyFormRef" 
          :model="resetpasswordForm" 
          :rules="verifyRules" 
          label-position="top"
          status-icon
          
        >
          <el-form-item prop="username" label="用户名">
            <el-input 
              v-model="resetpasswordForm.username" 
              prefix-icon="User"
              placeholder="请输入用户名"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="email" label="电子邮箱">
            <el-input 
              v-model="resetpasswordForm.email" 
              placeholder="请输入电子邮箱"
              prefix-icon="Message"
              clearable
            />
          </el-form-item>
          <el-form-item prop="newPassword" label="新密码">
            <el-input 
              v-model="resetpasswordForm.newPassword" 
              type="password"
              placeholder="请输入新密码"
              show-password
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword" label="确认密码">
            <el-input 
              v-model="resetpasswordForm.confirmPassword" 
              type="password"
              placeholder="请再次输入新密码"
              show-password
              clearable
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              :loading="loading" 
              @click="handleResetPassword"
              class="submit-btn"
            >
              重置密码
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="actions">
          <router-link to="/login" class="back-link">
            返回登录
          </router-link>
        </div>
        
        <el-alert
          v-if="message"
          :title="message"
          :type="isSuccess ? 'success' : 'error'"
          show-icon
          class="alert"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { usernameRules,emailRules,passwordRules } from '@/utils/validationRules'
import { ElMessage } from 'element-plus'
import { Phone, Message } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'
// 路由实例
const router = useRouter()

// 加载状态
const loading = ref(false)

// 消息状态
const message = ref('')
const isSuccess = ref(false)

// 验证身份表单引用
const verifyFormRef = ref(null)

// 验证身份表单数据
const resetpasswordForm = reactive({
  username: '',
  email: '',
  newPassword: '',
  confirmPassword: ''
})


// 验证身份表单规则
const verifyRules = {
  username: usernameRules,
  email: emailRules,
  newPassword:passwordRules,
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value !== resetpasswordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ]
}


// 忘记密码-重置密码√
const handleResetPassword = async () => {
  if (!verifyFormRef.value) return
  
  try {
    // 表单验证
    await verifyFormRef.value.validate()
    
    // 设置加载状态
    loading.value = true
    message.value = ''
    
    // 调用真实的API
    await useAuthStore().resetPassword(
      resetpasswordForm.username, 
      resetpasswordForm.email, 
      resetpasswordForm.newPassword
    );
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    // 模拟成功响应
    isSuccess.value = true
    message.value = '密码重置成功，请使用新密码登录'
    
    // 1秒后跳转到登录页
    setTimeout(() => {
      router.push('/login')
    }, 500)
    
  } catch (error) {
    if (error === false) {
      return
    }
    
    if (error?.fields) {
      // 表单验证错误
      const fieldError = error.fields[0]
      message.value = fieldError.message
      isSuccess.value = false
    } else {
      // API错误或其他错误
      message.value = '用户名或邮箱不正确，请检查输入信息'
      isSuccess.value = false
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.forgot-password-step {
  width: 500px;
}

.forgot-password-card {
  width: 100%;
  max-width: 450px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 24px;
  color: #303133;
  margin-bottom: 8px;
  text-align: center;
}

.subtitle {
  font-size: 14px;
  color: #909399;
  margin-bottom: 24px;
  text-align: center;
}

.submit-btn {
  width: 100%;
}

.actions {
  margin-top: 20px;
  text-align: center;
}

.back-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
}

.back-link:hover {
  text-decoration: underline;
}

.alert {
  margin-top: 16px;
}
</style>